package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.InvitationDAO;
import com.diankangwo.dao.MyEarningsDAO;
import com.diankangwo.dao.OrderDAO;
import com.diankangwo.dao.UserDAO;
import com.diankangwo.domain.Invitation;
import com.diankangwo.domain.MyEarnings;
import com.diankangwo.domain.Order;
import com.diankangwo.domain.UserInfo;
import com.diankangwo.dto.MyEarningsDTO;
import com.diankangwo.dto.invitationDto;
import com.diankangwo.util.PageSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分享控制器
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "分享")
public class InvitationController {

    @Autowired
    private InvitationDAO invitationDao;

    @Autowired
    private UserDAO userDao;

    @Autowired
    private MyEarningsDAO meDao;

    @Autowired
    private OrderDAO orderDAO;

    /**
     * 获取我的邀请信息
     *
     * @param accessToken token
     * @return
     */
    @ApiOperation(value = " 获取我的邀请信息", notes = "")
    @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String")
    @ResponseBody
    @RequestMapping(value = "/invitation", method = RequestMethod.GET)
    public String getInvitation(String accessToken) {
        Map<String, Object> mapJson = new HashMap<String, Object>();
        UserInfo user = userDao.getUserByToken(accessToken);
        PageSupport<Invitation> ivtList = null;
        double sumMoney = 0;
        String code = "2";
        String msg = "处理异常！";
        if (user != null) {
            ivtList = invitationDao.findByFUid(user.getUid(), 0, Integer.MAX_VALUE);
            mapJson.put("number_of_users", ivtList != null ? ivtList.getResultData().size() : null);
            mapJson.put("invitation_code", user.getUid());
            if (ivtList != null) {
                for (Invitation invitation : ivtList.getResultData()) {
                    if (invitation.getMy_earnings() > 0) {
                        sumMoney += invitation.getMy_earnings();
                    }
                    code = "1";
                    msg = "查询成功！";
                }
            } else {
                code = "1";
                msg = "未查询到数据！";
            }
            mapJson.put("profit", user.getCommissionbalance());
        } else {
            msg = "未获取到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, mapJson);
    }

    /**
     * 获取我的客户列表
     *
     * @param accessToken 用户UID
     * @return
     */
    @ApiOperation(value = " 获取我的客户列表", notes = "")
    @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String")
    @ResponseBody
    @RequestMapping(value = "/invitation/users", method = RequestMethod.GET)
    public String getInvitationUser(String accessToken, int pageNo, int pageSize) {
        List<invitationDto> ivtDList = new ArrayList<invitationDto>();
        invitationDto ivtD = new invitationDto();
        UserInfo user = userDao.getUserByToken(accessToken);
        PageSupport<Invitation> ivtList = invitationDao.findByFUid(user.getUid(), pageNo, pageSize);
        Double my_earnings_total = 0.0;
        Double spending_total = 0.0;
        String code = "2";
        String msg = "处理异常！";
        if (user != null) {
            if (ivtList != null) {
                for (Invitation invitation : ivtList.getResultData()) {
                    my_earnings_total += invitation.getMy_earnings();
                    spending_total += invitation.getSpending_total();
                }
                ivtD.setGender(user.getGender());
                ivtD.setMy_earnings_total(my_earnings_total);
                ivtD.setProfile_image_url(user.getProfileImageUrl());
                ivtD.setUglify_tel(user.getTel().substring(0, 3) + "****" + user.getTel().substring(7, user.getTel().length()));
                ivtD.setUid(user.getUid());
                ivtD.setUsername(user.getUsername());
                ivtD.setSpending_total(spending_total);
                ivtDList.add(ivtD);
                code = "1";
                msg = "查询成功！";
            } else {
                code = "1";
                msg = "未查询到分享信息！";
            }
        } else {
            msg = "未查询到用户信息！";
        }
        if (ivtList == null) {
            return resultMsg.mapToJson(code, msg, null);
        } else {
            return resultMsg.mapToJson(code, msg, ivtList.getResultData());
        }
    }


    /**
     * 获取我的收益列表
     *
     * @param accessToken token
     * @param fuid        用户UID
     * @return
     */
    @ApiOperation(value = " 获取我的收益列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "fuid", value = "用户UID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/invitation/profit", method = RequestMethod.GET)
    public String show(String accessToken, String fuid, int pageNo, int pageSize) {
        UserInfo user = userDao.getUserByToken(accessToken);
        String code = "2";
        String msg = "处理异常！";
        List<MyEarningsDTO> meList = new ArrayList<MyEarningsDTO>();
        PageSupport<Invitation> iList = null;
        if (user != null) {
            if (fuid.isEmpty() || fuid == "") {
//                iList = invitationDao.findByFUid(user.getUid(), pageNo, pageSize);
//                if (iList != null) {
//                    for (Invitation invitation : iList.getResultData()) {
//
//                    }
//                }
                List<MyEarnings> mList = meDao.getUserByUid(user.getUid());
                if (mList != null) {
                    for (MyEarnings myEarnings : mList) {
                        MyEarningsDTO m = new MyEarningsDTO();
                        m.setDate(myEarnings.getDate());
                        m.setId(myEarnings.getId());
                        m.setMy_earnings(myEarnings.getMy_earnings());
                        m.setOrder_id(myEarnings.getOrder_id());
                        m.setText(myEarnings.getText());
                        Order order = orderDAO.findByOrderNo(myEarnings.getOrder_id());
                        m.setUsername(order.getUserName());
                        m.setTel(order.getUserPhone());
                        meList.add(m);
                    }
                }
            } else {
                List<MyEarnings> mList = meDao.getUserByUid(fuid);
                for (MyEarnings myEarnings : mList) {
                    MyEarningsDTO m = new MyEarningsDTO();
                    m.setDate(myEarnings.getDate());
                    m.setId(myEarnings.getId());
                    m.setMy_earnings(myEarnings.getMy_earnings());
                    m.setOrder_id(myEarnings.getOrder_id());
                    m.setText(myEarnings.getText());
                    Order order = orderDAO.findByOrderNo(myEarnings.getOrder_id());
                    m.setUsername(order.getUserName());
                    m.setTel(order.getUserPhone());
                    meList.add(m);
                }
            }
            code = "1";
            if (meList.size() > 0) {
                msg = "查询成功！";
            } else {
                msg = "未查询到记录！";
            }
        } else {
            msg = "未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, meList);
    }
}
